import { login, getInfo, updateUserPassword } from '@/api/user'
import { getToken, setToken, removeToken, getUserId, setUserId } from '@/utils/auth'
import { resetRouter } from '@/router'

const state = {
  token: getToken(),
  name: '',
  username: '',
  avatar: '',
  email: '',
  roles: [], // roles的写法是为了兼容该UI框架
  role_list: [], // role_list则为用户实际的角色列表
  menu: [],
  line: '',
  user_id: getUserId(),
  training: false
}

const mutations = {
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_USER_ID: (state, user_id) => {
    state.user_id = user_id
    setUserId(user_id)
  },
  SET_NAME: (state, name) => {
    state.name = name
  },
  SET_USERNAME: (state, username) => {
    state.username = username
  },
  SET_EMAIL: (state, email) => {
    state.email = email
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar
  },
  SET_ROLES: (state, roles) => {
    state.roles = roles
  },
  SET_ROLE_LIST: (state, role_list) => {
    state.role_list = role_list
  },
  SET_LINE: (state, line) => {
    state.line = line;
  },
  SET_MENU: (state, menu) => {
    state.menu = menu
  },
  SET_TRAINING: (state, training) => {
    state.training = training
  }
}

const actions = {
  // user login
  login({ commit }, userInfo) {
    const { username, password, line } = userInfo
    return new Promise((resolve, reject) => {
      login({ username: username.trim(), password: password, line: line }).then(response => {
        const { data, token } = response
        commit('SET_TOKEN', token)
        commit('SET_USER_ID', data.id)
        setToken(token)
        resolve(response)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // get user info
  getInfo({ commit, state }) {
    return new Promise((resolve, reject) => {
      getInfo(state.user_id).then(response => {
        const { data } = response

        if (!data) {
          reject('Verification failed, please Login again.')
        }

        const { username, name, email, avatar, role_desc, is_admin, menu } = data

        commit('SET_NAME', name)
        commit('SET_USERNAME', username)
        commit('SET_EMAIL', email)
        commit('SET_ROLE_LIST', role_desc)
        if (is_admin) {
          commit('SET_ROLES', ['admin'])
        } else {
          commit('SET_ROLES', ['editor'])
        }
        commit('SET_MENU', menu)
        commit('SET_AVATAR', avatar)
        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  },

  updateUserPassword({ commit }, data) {
    return new Promise((resolve, reject) => {
      updateUserPassword(data).then(response => {
        resolve(response)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // user logout
  logout({ commit, state, dispatch }) {
    return new Promise((resolve, reject) => {
      commit('SET_TOKEN', '')
      commit('SET_ROLES', [])
      removeToken()
      resetRouter()

      // reset visited views and cached views
      // to fixed https://github.com/PanJiaChen/vue-element-admin/issues/2485
      dispatch('tagsView/delAllViews', null, { root: true })
      resolve()
    })
  },

  // remove token
  resetToken({ commit }) {
    return new Promise(resolve => {
      commit('SET_TOKEN', '')
      commit('SET_ROLES', [])
      removeToken()
      resolve()
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
